package bysj.service;

import bysj.dao.ApplicationForSupervisorDao;
import bysj.domain.ApplicationForSupervisor;
import util.BysjException;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;

public class ApplicationForSupervisorService {
    private static ApplicationForSupervisorDao applicationForSupervisorDao
            = ApplicationForSupervisorDao.getInstance();
    private static ApplicationForSupervisorService applicationForSupervisorService = null;
    private ApplicationForSupervisorService(){}

    public static ApplicationForSupervisorService getInstance(){
        if (applicationForSupervisorService == null){
            applicationForSupervisorService =new ApplicationForSupervisorService();
        }
        return applicationForSupervisorService;
    }

    public boolean add(ApplicationForSupervisor applicationForSupervisor) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            added = applicationForSupervisorDao.add(applicationForSupervisor, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务中所有操作
            connection.rollback();
            //抛出
           // throw new BysjException("未能同时添加所有志愿!");
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }
}
